package ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;

import db.Account;
import db.BankDatabase;

public class ViewBalance extends JPanel {
	private JLabel lblViewBalance, lblXA9, lblLogo;
	private JButton btnReturn;
	private JTable tblAccounts;
	private JScrollPane scpAccounts;
	private DefaultTableModel tableModel;
	private BankDatabase bankDatabase; // account information database
	
	public ViewBalance() {
		
		this.bankDatabase = new BankDatabase();

		setBackground(UIManager.getColor("Label.background"));
		setLayout(null);

		lblViewBalance = new JLabel("Balance overview");
		lblViewBalance.setOpaque(true);
		lblViewBalance.setBackground(UIManager.getColor("Label.background"));
		lblViewBalance.setHorizontalAlignment(SwingConstants.CENTER);
		lblViewBalance.setForeground(Color.BLACK);
		lblViewBalance.setFont(new Font("Eurostile", Font.BOLD, 24));
		lblViewBalance.setBounds(0, 94, 735, 28);
		add(lblViewBalance);

		btnReturn = new JButton("Return to main menu");
		btnReturn.setForeground(UIManager.getColor("CheckBox.select"));
		btnReturn.setFont(new Font("Eurostile", Font.PLAIN | Font.BOLD, 13));
		btnReturn.setBounds(156, 325, 200, 50);
		add(btnReturn);

		lblXA9 = new JLabel("\u00A92013 CBS HA.IT (Gruppe XA-9)\t\t ");
		lblXA9.setForeground(UIManager.getColor("Button.darkShadow"));
		lblXA9.setBackground(UIManager.getColor("Button.darkShadow"));
		lblXA9.setHorizontalAlignment(SwingConstants.CENTER);
		lblXA9.setFont(new Font("Eurostile", Font.PLAIN, 11));
		lblXA9.setBounds(0, 423, 735, 50);
		add(lblXA9);

		lblLogo = new JLabel();
		lblLogo.setIcon(new ImageIcon(
				"/Users/martinotto-sidelmann/Desktop/Zombium.png"));
		lblLogo.setBounds(530, 12, 200, 70);
		add(lblLogo);

		scpAccounts = new JScrollPane();
		scpAccounts.setBounds(159, 140, 415, 160);
		scpAccounts.setFont(new Font("Eurostile", Font.PLAIN, 13));
		add(scpAccounts);

		
		this.tableModel = new DefaultTableModel();
		
		tblAccounts = new JTable(this.tableModel);
		
		this.tableModel.addColumn("AccountId");
		this.tableModel.addColumn("Accountnumber");		
		this.tableModel.addColumn("Balance");		
		
		tblAccounts.setEnabled(false);
		scpAccounts.setViewportView(tblAccounts);
		tblAccounts.setBackground(Color.WHITE);
		tblAccounts.setRowHeight(25);
		tblAccounts.setFont(new Font("Eurostile", Font.PLAIN, 13));
		tblAccounts.getTableHeader();
	}

	public JButton getBtnReturn() {
		return btnReturn;
	}

	
	public void diplayAccounts(int loginId){
		
		System.out.println(loginId);
		
		ArrayList<Account> accounts = this.bankDatabase.getAccounts(loginId);
		
		for(Account account : accounts){
			System.out.println(account.getAccountId());
			Object[] data = {account.getAccountId(), account.getAccountName(), account.getAmount()};
			this.tableModel.addRow(data);
		}
	}
	
	public void addActionListener(ActionListener l) {
		btnReturn.addActionListener(l);
//		this.tableModel.removeRow(ABORT);

	}
}
